package com.shurdroides.proyectofinal.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.AccountsException;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.shurdroides.proyectofinal.constants.Constants;
import com.shurdroides.proyectofinal.constants.Constants.JSON;
import com.shurdroides.proyectofinal.databasehelpers.DataHelper;
import com.shurdroides.proyectofinal.preferences.MyPreferences;

public class Helper implements Constants {

	private static final String TAG = "Helper";
	private static MyPreferences preferences;
	private static DataHelper data;
	
	public static MyPreferences getMyPreferences(Context ctx) {
		if (preferences == null)
			preferences = new MyPreferences(ctx);
		return preferences;
	}

	public static DataHelper getDataHelper(Context mContext) {
		if (data == null) {
			data = new DataHelper(mContext);
			return data;
		} else {
			return data;
		}
	}

	public static DefaultHttpClient getClient() {
		DefaultHttpClient ret = null;

		ret = new DefaultHttpClient();
		return ret;
	}
	
	public static boolean isInternetEnabled(Context ctx) {
		return HttpUtils.isInternetEnabled(ctx);
	}


	

	/**
	 * 
	 * @param ctx
	 * @param query
	 * @return
	 */
	public static HttpEntity executeAppEngineRequest(Context ctx, String query) {
		InputStream i = null;

		HttpGet get = new HttpGet(AppEngine.APP + query);
		Log.e(TAG, AppEngine.APP + query);
		HttpResponse response = null;
		try {
			response = HttpUtils.getClient(ctx).execute(get,
					HttpUtils.getAuthenticatedContext(ctx, null));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (AccountsException e) {
			e.printStackTrace();
		}
		Toast.makeText(ctx, response.toString(), Toast.LENGTH_SHORT).show();

		return response.getEntity();
	}

	/**
	 * 
	 * @param ctx
	 * @param query
	 * @return
	 */
	private static InputStream executeAppEngineFileRequest(Context ctx,
			String query) {
		InputStream result = null;
		try {
			result = ((HttpEntity) executeAppEngineRequest(ctx, query))
					.getContent();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	 * @param ctx
	 * @param query
	 * @return
	 */
	public static JSONObject executeAppEngineJSONRequest(Context ctx,
			String query) {
		JSONObject jsonObject = null;
		String result = "NO RESULT";
		try {
			result = EntityUtils.toString(executeAppEngineRequest(ctx, query));
			jsonObject = new JSONObject(result);
			// getToonsPreferences(ctx).updateVersion(
			// jsonObject.getString("version"),
			// jsonObject.getString("s").length());
		} catch (JSONException e) {
			Log.e(TAG, "ERROR TO PARSE JSON RESPONSE  - response = " + result);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonObject;
	}

	private static String convertInputStreamToString(InputStream i) {
		StringBuffer sb = new StringBuffer();
		String line;

		BufferedReader rd = null;
		try {
			rd = new BufferedReader(new InputStreamReader(i));
			if (rd != null) {
				while ((line = rd.readLine()) != null) {
					sb.append(line);
				}

				rd.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return sb.toString();

	}
	
	public static JSONArray urlToJsonArray(String url) {
		JSONArray jsonAr = new JSONArray();
		try {
			HttpClient cli = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);
			HttpResponse response = cli.execute(get);
			String json = Helper.convertInputStreamToString(response.getEntity().getContent());
			jsonAr = new JSONArray(json);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonAr;
	}

	
	
	public static int getRespuestaMensaje(String json) {
		int res=-99;
		try {
			JSONObject j=new JSONObject(json);
			res= j.getInt(JSON.TIPO_MENSAJE);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	
}
